//
//  AppDelegate.m
//  A_project2.0
//
//  Created by Tiny on 16/3/7.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "AppDelegate.h"
#import "OMGHomeViewController.h"
#import "OMGSiteViewController.h"
#import "OMGGroupViewController.h"
#import "OMGMyViewController.h"
#import "JPUSHService.h"

#import "WeiboSDK.h"

@interface AppDelegate ()<UITabBarControllerDelegate, WeiboSDKDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    
    
    BOOL isFirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
    if (!isFirst) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
        GuideViewController *userVC = [[GuideViewController alloc]init];
        self.window.rootViewController  = userVC;
    self.tabBar = [[UITabBarController alloc]init];
    
    OMGHomeViewController *homeVC = [[OMGHomeViewController alloc]init];
    UINavigationController *homeNavi = [[UINavigationController alloc]initWithRootViewController:homeVC];
    homeVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"推荐" image:[UIImage imageNamed:@"home"] tag:1000];
    
    OMGSiteViewController *siteVC = [[OMGSiteViewController alloc]init];
    UINavigationController *siteNavi = [[UINavigationController alloc]initWithRootViewController:siteVC];
    siteVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"目的地" image:[UIImage imageNamed:@"site"] tag:1001];
    
    OMGGroupViewController *groupVC = [[OMGGroupViewController alloc]init];
    UINavigationController *groupNavi = [[UINavigationController alloc]initWithRootViewController:groupVC];
    groupVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[UIImage imageNamed:@"group"] tag:1002];
    
    OMGMyViewController *myVC = [[OMGMyViewController alloc]init];
    UINavigationController *myNavi = [[UINavigationController alloc]initWithRootViewController:myVC];
    myVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"my"] tag:1003];
    
    _tabBar.viewControllers = @[homeNavi, siteNavi, groupNavi, myNavi];
        _tabBar.delegate = self;
    }else{
        UITabBarController *tabBar = [[UITabBarController alloc]init];
        
        OMGHomeViewController *homeVC = [[OMGHomeViewController alloc]init];
        UINavigationController *homeNavi = [[UINavigationController alloc]initWithRootViewController:homeVC];
        homeVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"推荐" image:[UIImage imageNamed:@"home"] tag:1000];
        
        OMGSiteViewController *siteVC = [[OMGSiteViewController alloc]init];
        UINavigationController *siteNavi = [[UINavigationController alloc]initWithRootViewController:siteVC];
        siteVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"目的地" image:[UIImage imageNamed:@"site"] tag:1001];
        
        OMGGroupViewController *groupVC = [[OMGGroupViewController alloc]init];
        UINavigationController *groupNavi = [[UINavigationController alloc]initWithRootViewController:groupVC];
        groupVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[UIImage imageNamed:@"group"] tag:1002];
        
        OMGMyViewController *myVC = [[OMGMyViewController alloc]init];
        UINavigationController *myNavi = [[UINavigationController alloc]initWithRootViewController:myVC];
        myVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"my"] tag:1003];
        
        tabBar.viewControllers = @[homeNavi, siteNavi, groupNavi, myNavi];
        self.window.rootViewController = tabBar;
    }
   

    [self.window makeKeyAndVisible];
    
    /**< 极光推送 */
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
    }else{
        // categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound categories:nil];
    }
    // Required
    [JPUSHService setupWithOption:launchOptions appKey:@"3b2ad1649e550f4bdf9e94b7" channel:@"publish channel" apsForProduction:0];
    
    
    //新浪微博

    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:kAppKey];
    
    
    return YES;
}

//新浪微博 重写AppDelegate 的handleOpenURL和openURL⽅方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

//新浪微博必须实现的 不写崩溃
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    
}



- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
    NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
}

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary * _Nullable))reply
{
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"%@",userInfo);
    
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@",
          error);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
